/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pt.iasd.youthshelter.pathfinder.nodes;

import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;
import pt.iasd.youthshelter.datacore.controller.exceptions.NonexistentEntityException;
import pt.iasd.youthshelter.datamodel.Pathfinder;
import pt.iasd.youthshelter.datamodel.service.PathfinderController;
import pt.iasd.youthshelter.logging.ILoggingService;
import pt.iasd.youthshelter.pathfinder.PathfinderEditorTopComponent;

/**
 *
 * @author Rui
 */
public class PathfinderNode extends AbstractNode {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/pathfinder/Bundle");
    
    private final Pathfinder pathfinder;
    
    public PathfinderNode(Pathfinder pathfinder) {
        super(Children.LEAF);
        this.pathfinder = pathfinder;
        setDisplayName(this.pathfinder.getName());
    }

    @Override
    public Action getPreferredAction() {
        return new OpenPathfinderAction();
    }

    @Override
    public Action[] getActions(boolean context) {
        return new Action[]{new OpenPathfinderAction(), new ArchivePathfinderAction()};
    }
    
    private class OpenPathfinderAction extends AbstractAction {
        public OpenPathfinderAction() {
            putValue(NAME, bundle.getString("Action.Open"));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            TopComponent editor = new PathfinderEditorTopComponent(pathfinder);
            editor.open();
            editor.requestActive();
        }
    }
    
    private class ArchivePathfinderAction extends AbstractAction {
        public ArchivePathfinderAction() {
            putValue(NAME, bundle.getString("Action.Archive"));
        }
        
        @Override
        public void actionPerformed(ActionEvent e) {
            PathfinderController controller = Lookup.getDefault().lookup(PathfinderController.class);
            if(controller != null) {
                NotifyDescriptor.Confirmation confirm = new NotifyDescriptor.Confirmation(
                        bundle.getString("Archive.Pathfinder.Message"),
                        bundle.getString("Archive.Pathfinder.Title")
                );
                if(DialogDisplayer.getDefault().notify(confirm) == NotifyDescriptor.OK_OPTION) {
                    pathfinder.setEndActive(new Date());
                    try {
                        controller.edit(pathfinder);
                        //PathfinderListTopComponent.loadPathfinderListData();
                    } catch (NonexistentEntityException ex) {
                        controller.create(pathfinder);
                        Lookup.getDefault().lookup(ILoggingService.class).warn(this.getClass(), ex);
                    } catch (Exception ex) {
                        NotifyDescriptor.Message alert = new NotifyDescriptor.Message(
                        bundle.getString("Archive.Pathfinder.Error")
                                .replace("\n", System.getProperty("line.separator")),
                        NotifyDescriptor.ERROR_MESSAGE);
                        DialogDisplayer.getDefault().notify(alert);
                        Lookup.getDefault().lookup(ILoggingService.class).error(this.getClass(),
                                bundle.getString("Archive.Pathfinder.Error")
                                        .replace("\n", System.getProperty("line.separator")), ex);
                    }
                }
            } else {
                NotifyDescriptor.Message alert = new NotifyDescriptor.Message(
                        bundle.getString("NoController.Error")
                                .replace("\n", System.getProperty("line.separator")),
                        NotifyDescriptor.ERROR_MESSAGE);
                DialogDisplayer.getDefault().notify(alert);
                Lookup.getDefault().lookup(ILoggingService.class).error(this.getClass(),
                        bundle.getString("NoController.Error").replace("\n", System.getProperty("line.separator")));
            }
        }
    }
}
